import { getPlayerData, isPlayerExist } from '../l.js'
import puppeteer from "../../../lib/puppeteer/puppeteer.js";

export class Fs extends plugin {
    constructor() {
        super({
            name: '[Fs]农田信息',
            dsc: '农田信息',
            event: 'message',
            priority: 1,
            rule: [
                { reg: /^(#|\/)?农田信息$/, fnc: 'farmlandInformation' }
            ]
        })
    }

    async farmlandInformation(e) {
        const PLAYER_ID = e.user_id
        if (!isPlayerExist(PLAYER_ID)) {
            return e.reply(['无玩家信息，请发送[领取田地]喵~'])
        }
        const PLAYER_DATA = await getPlayerData(PLAYER_ID)

        let info = []
        for (let i = 0; i < PLAYER_DATA['frame'].length; i++) {
            const frame = PLAYER_DATA['frame'][i]
            const START = frame['start']

            if (START) {
                const currentTime = Math.floor(Date.now() / 1000)
                const remainingTime = frame['time'] - currentTime
                const timeStr = remainingTime > 0
                    ? `剩余${Math.ceil(remainingTime / 60)}分钟`
                    : '已成熟'

                info.push({
                    number: i + 1,
                    name: frame['name'],
                    time: timeStr,
                    harvest: frame['number'],
                    fertilizer: frame['fertilizer'] || null,
                    status: remainingTime > 0 ? 'growing' : 'mature'
                })
            } else {
                info.push({
                    number: i + 1,
                    name: null,
                    time: null,
                    harvest: null,
                    fertilizer: null,
                    status: 'empty'
                })
            }
        }

        const image = await puppeteer.screenshot('农田信息', {
            tplFile: 'plugins/Farming-Simulator/resources/html/farmlandInformation.html',
            info,
            style: {
                width: Math.ceil(Math.sqrt(info.length)) * 210,
                gridTemplateColumns: `repeat(${Math.ceil(Math.sqrt(info.length))}, 180px)`
            }
        })

        return e.reply([image])
    }
}